package com.yanqu.road.server.cmd.chefduel.process2;

import com.yanqu.road.entity.activity.chefduel.CrossChefDuel2Rank;
import com.yanqu.road.entity.activity.chefduel.CrossChefDuel2RankListModel;
import com.yanqu.road.entity.activity.chefduel.process2.ChefDuelUpUser;
import com.yanqu.road.entity.activity.chefduel.rank.CampRank;
import com.yanqu.road.entity.activity.chefduel.rank.CampRankListModel;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.CrossRelationActivityMgr;
import com.yanqu.road.server.manager.chefduel.CrossChefDuel2;
import com.yanqu.road.server.manager.chefduel.CrossChefDuelMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.LinkedList;

@Cmd(code = Protocol.C_CHEF_DUEL2_CAMP_RANK_LIST, desc = "阵营排行")
public class CrossChefDuel2CampRankListCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ChefDuelProto.ChefDuel2CampRankListReqMsg reqMsg = ChefDuelProto.ChefDuel2CampRankListReqMsg.parseFrom(packet.getBytes());

        ChefDuelProto.ChefDuel2CampRankListRespMsg.Builder clientMsg = ChefDuelProto.ChefDuel2CampRankListRespMsg.newBuilder();
        clientMsg.setRet(0);
        clientMsg.setMyCampId(-1);
        clientMsg.setMyRank(-1);
        clientMsg.setIsGetReward(true);
        CrossChefDuel2 crossChefDuel2 = CrossChefDuelMgr.getCrossChefDuel2IfNull(reqMsg.getActivityId());
        if(crossChefDuel2 != null){
            ChefDuelUpUser duelUpUser = crossChefDuel2.getChefDuelMap().get(packet.getPlayerId());
            if(duelUpUser != null && duelUpUser.getCampId() > 0){
                clientMsg.setMyCampId(duelUpUser.getCampId());
                clientMsg.setIsGetReward(duelUpUser.getCampRewardGet() == 1);
            }
            CampRankListModel listModel = crossChefDuel2.getCampRankListModel();
            LinkedList<CampRank> rankList = listModel.getRankList();
            int conditionType = CrossRelationActivityMgr.getEventConditionType(eGamePlayerEventType.ChefDuel2IntegralUserCrossRank.getValue(), CrossRelationActivityMgr.EVENT_RANK_USER_TYPE);
            int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossRelationActivityMgr.getActivityConditionInfoList(reqMsg.getActivityId()));
            for (int i = 0; i < rankList.size(); i++) {
                CampRank entry = rankList.get(i);
                if(clientMsg.getMyCampId() == entry.getId()){
                    clientMsg.setMyRank(i + 1);
                }
                ChefDuelProto.ChefDuel2CampRankTemp.Builder temp = ChefDuelProto.ChefDuel2CampRankTemp.newBuilder();
                temp.setCampId(entry.getId());
                temp.setScore(entry.getValue());

                CrossChefDuel2RankListModel model = crossChefDuel2.getCampUserRankMap().get(entry.getId());
                if(model != null){
                    int count = 0;
                    for (CrossChefDuel2Rank rank : model.getRankList()) {
                        ChefDuelProto.ChefDuel2CampUserRankTemp.Builder userTemp = ChefDuelProto.ChefDuel2CampUserRankTemp.newBuilder();
                        userTemp.setUserId(rank.getId());
                        userTemp.setScore(rank.getValue());
                        UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(rank.getId());
                        if(userBaseInfo != null){
                            userTemp.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                            temp.addUser(userTemp);
                        }
                        count++;
//                        if(count > 50 && count > maxRank / 2){
//                            break;
//                        }
                    }
                }
                clientMsg.addRank(temp);
            }
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_CHEF_DUEL2_CAMP_RANK_LIST, clientMsg));
    }

    @Override
    public void sendData() {

    }
}
